
#include "StdAfx.h"
#include "EMap.h"
#include "AppearanceServer.h"
#include "MonitorPlatform.h"
#include "EMapMainDlg.h"

bool CompareSortInMap(const CEMap* _left, const CEMap* _right)
{
	//return (_left->GetOrderInMap() < _right->GetOrderInMap());
	return TRUE;
}

CEMap::CEMap(MapInfo& MapProperty)
: m_MapProperty(MapProperty)
, m_pParentMap(NULL)
{
}

CEMap::~CEMap(void)
{
}

void CEMap::GetMapProperty(MapInfo* pProperty) const
{
	*pProperty = m_MapProperty;
}

BOOL CEMap::UpdateMapProperty(MapInfo& MapProperty, std::string pData)
{
	CString strPicName;
	int nId = 0;
	Result res = AAS_EditMapInfo(theUser->GetUserName(), theUser->GetToken(), MapProperty, 3, pData, strPicName, nId);
	if (res.code == AAS_ERROR_SUCCESS)
	{
		MapProperty.id = nId;
		MapProperty.picpath = strPicName;
		m_MapProperty = MapProperty;
		return TRUE;
	}
	else
	{
		CString strLog, strMsg;
		strMsg = LOAD_STRING(_T("ID_string_modify_map_failed"));
		strLog.Format(LOAD_STRING(_T("ID_string_AAS_err_value")), strMsg, _T("ModifyEMap"), res.code);
		theLog.WriteLog(strLog);

		CEMapMainDlg* pDlg = (CEMapMainDlg*)thePlatform.GetGlobalObj(GLOBAL_EMAP_MAINDLG);
		::MessageBox(pDlg->m_hWnd, strLog, _T("CDM Client"), MB_OK);
		return FALSE;
	}
}

BOOL CEMap::AddChildMap(CEMap* pMap)
{
	if(pMap->GetParentMapID() == m_MapProperty.id)
	{
		m_ChildMapList.push_back(pMap);
		pMap->SetParentMap(this);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void CEMap::SetParentMap(CEMap* pParentMap)
{
	if(GetParentMapID() == pParentMap->GetMapID())
	{
		m_pParentMap = pParentMap;
	}
}

void CEMap::RemoveMap(CEMap* pMap)
{
	m_ChildMapList.remove(pMap);
}

void CEMap::SortChildMapList()
{
	m_ChildMapList.sort(CompareSortInMap);
}

BOOL CEMap::IncreaseOrder(CEMap* pMap)
{
	//CEMapPtrList::iterator it = find(m_ChildMapList.begin(), m_ChildMapList.end(), pMap);

	//if(it == m_ChildMapList.end())
	//{
	//	//theErrorLog.SetLastError(_T("invalid map object!!!"));
	//	return FALSE;
	//}

	//++it;

	//if(it == m_ChildMapList.end())
	//{
	//	//theErrorLog.SetLastError(LOAD_STRING(_T("ID_string_is_bottom_order")));
	//	return FALSE;
	//}

	//int nOrder = pMap->GetOrderInMap();

	//if(!pMap->SetOrderInMap(nOrder+1) ||
	//	!(*it)->SetOrderInMap(nOrder))
	//{
	//	return FALSE;
	//}

	//SortChildMapList();

	return TRUE;
}

BOOL CEMap::DecreaseOrder(CEMap* pMap)
{
	//CEMapPtrList::iterator it = find(m_ChildMapList.begin(), m_ChildMapList.end(), pMap);

	//if(it == m_ChildMapList.end())
	//{
	//	//theErrorLog.SetLastError(_T("invalid region object!!!"));
	//	return FALSE;
	//}

	//if(it == m_ChildMapList.begin())
	//{
	//	//theErrorLog.SetLastError(LOAD_STRING(_T("ID_string_is_top_order")));
	//	return FALSE;
	//}

	//--it;

	//int nOrder = pMap->GetOrderInMap();

	//if(!pMap->SetOrderInMap(nOrder-1) ||
	//	!(*it)->SetOrderInMap(nOrder))
	//{
	//	return FALSE;
	//}

	//SortChildMapList();

	return TRUE;
}

BOOL CEMap::IsTopOrder(CEMap* pMap)
{
	return (pMap == m_ChildMapList.front());
}

BOOL CEMap::IsBottomOrder(CEMap* pMap)
{
	return (pMap == m_ChildMapList.back());
}

//BOOL CEMap::SetOrderInMap(int nOrder)
//{
	//if(!theDBAccess.UpdateMapOrder(GetMapID(), nOrder))
	//{
	//	return FALSE;
	//}

//	m_MapProperty.nOrderInMap = nOrder;
//	return TRUE;
//}

//0407
//BOOL CEMap::AddDevice(CCDMDevice* pDevice)
//{
//	if(pDevice == NULL)
//	{
//		return FALSE;
//	}
//
//	if(pDevice->GetMap() != this)
//	{
//		return FALSE;
//	}
//
//	m_DeviceList.push_back(pDevice);
//	return TRUE;
//}
//
//BOOL CEMap::RemoveDevice(CCDMDevice* pDevice)
//{
//	if(pDevice == NULL)
//		return FALSE;
//
//	m_DeviceList.remove(pDevice);
//	return TRUE;
//}

BOOL CEMap::AddChannel(CChannel* pChannel)
{
	if(pChannel == NULL)
	{
		return FALSE;
	}

	if(pChannel->GetMap() != this)
	{
		return FALSE;
	}

	m_ChannelList.push_back(pChannel);
	return TRUE;
}

BOOL CEMap::RemoveChannel(CChannel* pChannel)
{
	if(pChannel == NULL)
		return FALSE;

	m_ChannelList.remove(pChannel);
	return TRUE;
}
